package com.kehao.leetcode.jzoffer;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**
 * @author Kehao.Chen
 * @version 1.0
 * @date 2022/6/23 20:52
 */
public class JZSolution030 {
    private static class RandomizedSet {

        Set<Integer> set = new HashSet();
        Random random = new Random();

        /** Initialize your data structure here. */
        public RandomizedSet() {

        }

        /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
        public boolean insert(int val) {
            if(set.contains(val)){
                return false;
            }else {
                set.add(val);
                return true;
            }
        }

        /** Removes a value from the set. Returns true if the set contained the specified element. */
        public boolean remove(int val) {
            if(set.contains(val)){
                set.remove(val);
                return true;
            }else {
                return false;
            }
        }

        /** Get a random element from the set. */
        public int getRandom() {
            Object[] objects = set.toArray();
            return (Integer) objects[random.nextInt(objects.length)];
        }
    }
}
